/*
 * Copyright (c) 2013-2018 上海汇数数据. All rights reserved.
 * @(#) RegionLevel.java 2018-08-19 12:51
 */

package cn.jh.common.core.cn;

import cn.jh.common.core.domain.EnumValue;
import cn.jh.common.core.domain.TextValue;
import cn.jh.common.core.i18n.I18n;

import java.util.Locale;


/**
 * 中国行政区域划分枚举（只引入三级，实际有5级）。
 *
 * <a href="https://en.wikipedia.org/wiki/Administrative_divisions_of_China">Administrative divisions of China</a>
 *
 * @author Fuchun
 * @since 1.0
 */
public enum RegionLevel implements EnumValue<String>, TextValue<String, String> {

    /**
     * 省级行政区(Provincial level: 1st)
     * <pre>
     * 省 Provinces
     * 自治区 Autonomous regions
     * 直辖市 Municipalities
     * 特别行政区 Special administrative regions
     * </pre>
     */
    STATE("State", "region.type.state", "省（区、直辖市）", "省级行政区/一级行政区"),

    /**
     * 地级行政区(Prefectural level: 2nd)
     * <pre>
     * 地级市 Prefecture-level cities
     * 地区 Prefectures
     * 自治州 Autonomous prefectures
     * 盟 Leagues
     * </pre>
     */
    CITY("City", "region.type.city", "市（区、州）", "地级行政区/二级行政区"),

    /**
     * 县级行政区(County level: 3rd)
     * <pre>
     * 市辖区 Districts
     * 县级市 County-level cities
     * 县 Counties
     * 自治县 Autonomous counties
     * 旗 Banners
     * 自治旗 Autonomous banners
     * </pre>
     */
    COUNTY("County", "region.type.county", "县（区、旗）", "县级行政区/三级行政区")
    ;

    final String value;
    final String i18nCode;
    final String defaultText;
    final String description;

    RegionLevel(String value, String i18nCode, String defaultText, String description) {
        this.value = value;
        this.i18nCode = i18nCode;
        this.defaultText = defaultText;
        this.description = description;
    }

    @Override
    public String getText() {
        return getText(Locale.getDefault());
    }

    @Override
    public String getValue() {
        return value;
    }

    public String getText(Locale locale) {
        return I18n.getOrDefault(i18nCode, locale, defaultText);
    }

    public String getI18nCode() {
        return i18nCode;
    }

    public String getDefaultText() {
        return defaultText;
    }

    public String getDescription() {
        return description;
    }

    public static RegionLevel forString(String input) {
        String v, name;
        if (input == null || (v = input.trim()).isEmpty()) {
            throw new IllegalArgumentException("The input (" + input + ") is not in RegionLevel.");
        }
        name = v.toUpperCase();
        for (RegionLevel level : values()) {
            if (v.equals(level.value) || name.equals(level.name())) {
                return level;
            }
        }
        throw new IllegalArgumentException("The input (" + input + ") is not in RegionLevel.");
    }
}
